﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class GazedObject : MonoBehaviour, IVRInteractableObj
{
    public float gazeTime = 3f;
    protected SteamVR_TrackedObject.EIndex selectIndex = SteamVR_TrackedObject.EIndex.Hmd;
    public bool enableGaze
    {
        get { return _enableGaze; }
        set
        {
            _enableGaze = value;
            VRManager.manager.gazePoint.SetGazePointActive(value);
        }
    }
    protected bool _enableGaze;
    public UnityEvent onCompleted;
    //bool isRayIn = false;
    protected float timer = 0;

    public void ActiveGaze()
    {
        enableGaze = true;
    }
    public void OnRayIn(VRRayCastArgs rayArgs)
    {
        if (enableGaze && rayArgs.trackedObj.index == selectIndex)
        {
            VRManager.manager.gazePoint.LightPoint(true);
        }
    }

    public void OnRayOut(VRRayCastArgs rayArgs)
    {
        if (rayArgs.trackedObj.index == selectIndex)
        {
            //timer = 0;
            VRManager.manager.gazePoint.LightPoint(false);
        }
    }

    public void OnRayStay(VRRayCastArgs rayArgs)
    {
        if (enableGaze && rayArgs.trackedObj.index == selectIndex)
        {
            timer += Time.deltaTime;
            //Manager.manager.gazePoint.SetPosition(rayArgs.hit.point);
            VRManager.manager.gazePoint.SetProgress(timer / gazeTime);
            if (timer > gazeTime)
            {
                onCompleted.Invoke();
                enableGaze = false;
                timer = 0;
            }
        }
    }
}
